package yongxin.controller.purchaseSheet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import yongxin.entity.purchaseSheet.PurchaseSheetEntity;
import yongxin.service.purchaseSheet.PurchaseSheetServiceI;

/**
 * @Title: Controller
 * @Description: 采购申请单
 * @author zhangdaihao
 * @date 2015-04-09 13:14:52
 * @version V1.0
 * 
 */
@Controller
@RequestMapping("/purchaseSheetController")
public class PurchaseSheetController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(PurchaseSheetController.class);

	@Autowired
	private PurchaseSheetServiceI purchaseSheetService;
	@Autowired
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 采购申请单列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "purchaseSheet")
	public ModelAndView purchaseSheet(HttpServletRequest request) {
		return new ModelAndView("yongxin/purchaseSheet/purchaseSheetList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(PurchaseSheetEntity purchaseSheet,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(PurchaseSheetEntity.class,
				dataGrid);
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				purchaseSheet, request.getParameterMap());
		this.purchaseSheetService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除采购申请单
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(PurchaseSheetEntity purchaseSheet,
			HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		purchaseSheet = systemService.getEntity(PurchaseSheetEntity.class,
				purchaseSheet.getId());
		message = "采购申请单删除成功";
		purchaseSheetService.delete(purchaseSheet);
		systemService.addLog(message, Globals.Log_Type_DEL,
				Globals.Log_Leavel_INFO);

		j.setMsg(message);
		return j;
	}

	/**
	 * 添加采购申请单
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(PurchaseSheetEntity purchaseSheet,
			HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(purchaseSheet.getId())) {
			message = "采购申请单更新成功";
			PurchaseSheetEntity t = purchaseSheetService.get(
					PurchaseSheetEntity.class, purchaseSheet.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(purchaseSheet, t);
				purchaseSheetService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE,
						Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "采购申请单更新失败";
			}
		} else {
			message = "采购申请单添加成功";
			purchaseSheetService.save(purchaseSheet);
			systemService.addLog(message, Globals.Log_Type_INSERT,
					Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 采购申请单列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(PurchaseSheetEntity purchaseSheet,
			HttpServletRequest req) {
		if (StringUtil.isNotEmpty(purchaseSheet.getId())) {
			purchaseSheet = purchaseSheetService.getEntity(
					PurchaseSheetEntity.class, purchaseSheet.getId());
			req.setAttribute("purchaseSheetPage", purchaseSheet);
		}
		return new ModelAndView("yongxin/purchaseSheet/purchaseSheet");
	}

	/*
	 * 审批通过
	 */
	@RequestMapping(params = "checkSelect")
	@ResponseBody
	public AjaxJson checkSelect(PurchaseSheetEntity purchaseSheet,
			HttpServletRequest req) {
		AjaxJson j = new AjaxJson();

		String ids = req.getParameter("ids");
		String[] a = ids.split(",");

		if (a.length > 1) {
			for (int i = 0; i < a.length; i++) {
				String sql = " update t_purchase_sheet set fstate = '1' where id = ? ";
				systemService.executeSql(sql, a[i]);
			}
		} else {
			String sql = " update t_purchase_sheet set fstate = '1' where id = ? ";
			systemService.executeSql(sql, a[0]);
		}
		message = "采购申请单审批成功";
		int row = 1;
		if (row > 0) {
			message = "采购申请单审批成功";
		} else {
			message = "采购申请单审批失败";
		}
		j.setMsg(message);
		return j;
	}
	
	
	/*
	 * 审批通过
	 */
	@RequestMapping(params = "unCheckSelect")
	@ResponseBody
	public AjaxJson unCheckSelect(PurchaseSheetEntity purchaseSheet,
			HttpServletRequest req) {
		AjaxJson j = new AjaxJson();

		String ids = req.getParameter("ids");
		String[] a = ids.split(",");

		if (a.length > 1) {
			for (int i = 0; i < a.length; i++) {
				String sql = " update t_purchase_sheet set fstate = '2' where id = ? ";
				systemService.executeSql(sql, a[i]);
			}
		} else {
			String sql = " update t_purchase_sheet set fstate = '2' where id = ? ";
			systemService.executeSql(sql, a[0]);
		}
		message = "采购申请单审批成功";
		int row = 1;
		if (row > 0) {
			message = "采购申请单审批成功";
		} else {
			message = "采购申请单审批失败";
		}
		j.setMsg(message);
		return j;
	}
}
